﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.DirectoryServices.Protocols;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WLYT.Application.Interfaces;
using WLYT.Domain.Model;
using WLYT.Domain.Model.HikModel;

namespace WLYT.Application.Services
{
    public class HikService(IHttpWebService httpWebService) : IHikService
    {
        public async Task<ApiResult> GenAgvSchedulingTask(string reqCode, string taskTyp, List<string> positionList)
        {
            ApiResult result = new();
            if (string.IsNullOrEmpty(AppConfig.HikUrl))
            {
                return result;
            }
            List<HKAgvPositionModel> agvPosition = new List<HKAgvPositionModel>();
            foreach (var position in positionList)
            {
                /// 05：CTU、叉车库位 （仓位编号）
                /// 00：潜伏（位置编号）
                /// 02：区域策略（空货架区域点）
                agvPosition.Add(new HKAgvPositionModel { positionCode = position, type = "00" });
            }
            HKAgvRequestModel param = new HKAgvRequestModel
            {
                reqCode = reqCode,  //请求编码
                taskCode = reqCode, //任务编码
                taskTyp = taskTyp,  //任务类型
                positionCodePath = agvPosition, //位置点位
            };
            try
            {
                var apiResult = await httpWebService.HttpPostAsync<HKAgvResponseModel>(AppConfig.HikUrl + "genAgvSchedulingTask", param);
                if (apiResult.code != "0")
                {
                    result.Code = 1;
                    result.Message = apiResult.message;
                }
            }
            catch (Exception ex)
            {
                result.Code = 1;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// AGV取消任务
        /// </summary>
        /// <param name="reqCode">请求编码</param>
        /// <param name="taskCode">要取消的任务编号</param>
        /// <returns></returns>
        public async Task<ApiResult> CancelTask(string reqCode, string taskCode)
        {
            ApiResult result = new();
            var param = new HKAgvRequestModel
            {
                reqCode = reqCode,
                taskCode = taskCode
            };
            var apiResult = await httpWebService.HttpPostAsync<HKAgvResponseModel>(AppConfig.HikUrl + "genAgvSchedulingTask", param);
            if(apiResult.code != "0") 
            {
                result.Code = 1;
                result.Message = apiResult.message;
            }
            return result;
        }

        /// <summary>
        /// 获取转义后容器编码
        /// 容器编码不能带“*/?"<|'%>&”
        /// </summary>
        /// <param name="ctnrCode"></param>
        /// <returns></returns>
        private string GetCtnrCode(string ctnrCode)
        {
            if (string.IsNullOrEmpty(ctnrCode))
                return ctnrCode;

            return ctnrCode.Replace("*", "@").Replace("|", "-");
        }

        /// <summary>
        /// 继续任务
        /// </summary>
        /// <param name="taskCode"></param>
        /// <returns></returns>
        public async Task<ApiResult> ContinueTask(string taskCode)
        {
            ApiResult result = new();
            if (string.IsNullOrEmpty(AppConfig.HikUrl)) return result;
            string url = AppConfig.HikUrl + "continueTask";

            var param = new
            {
                reqCode = DateTime.Now.ToString("yyyyMMddHHmmssfff"),
                taskCode,
            };
            var apiResult = await httpWebService.HttpPostAsync<HKAgvResponseModel>(url, param);
            if (apiResult.code != "0")
            {
                result.Code = 1;
                result.Message = apiResult.message;
            }
            return result;
        }
    }
}
